//
//  rowViewModel.swift
//  swiftDatatable
//
//  Created by 李朋 on 17/3/14.
//  Copyright © 2017年 李朋. All rights reserved.
//

import UIKit

class rowViewModel: NSObject {
    
    static let sharedModel = rowViewModel()
    
    var dataArray:[[Any]] = [[Any]]()
    
    fileprivate lazy var itemWiths = [CGFloat]()
    
    fileprivate lazy var texts = [[String]]()

    override init() {
        dataArray = exampleDataSet()
        super.init()
        calculateWidth()
    }
    
    func getItemWithIndexPath(indexPath:Int) -> CGFloat {
        return itemWiths[indexPath] + getWidthWithString()
    }
    
    func getWidthWithString() -> CGFloat {
        return 20;
    }
    
    func calculateWidth() {
        texts = dataArray.map{
            return $0.map{
                if $0 is String {
                    return $0 as! String
                }else {
                    return String(describing: $0)
                }
            }
        }
        
        let size = CGSize(width: Double(MAXFLOAT), height: 60.0)
        
        let font = UIFont.systemFont(ofSize: 17)
        
        let dict = [NSFontAttributeName:font]
        
        var c = [[CGFloat]]()
        
        c = texts.map {
            
           return $0.map {
            
            let text = $0 as NSString
            
            return text.boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: dict, context: nil).size.width
            }
            
        }
        
        var max: CGFloat = 0.0
        for col in 0..<c[0].count {
            max = 0.0
            _ = c.map {
                if max < $0[col] {
                    max = $0[col]
                }
            }
            itemWiths.append(max)
        }
        
    }
}


// MARK: 排序
extension rowViewModel {
    func  sort(type:DataTableSortType) {
        switch type {
        case .ascending: break
        case .descending: break
        case .unspecified: break
        }
    }
}











